package com.ice.client.proto;

import com.google.protobuf.InvalidProtocolBufferException;
import com.ice.remoting.data.RemotingCommand;
import com.ice.remoting.netty.NettyRequestProcessor;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ProtoDefaultProcess implements NettyRequestProcessor {

    private ProtobufClient protobufClient;

    public ProtoDefaultProcess(ProtobufClient protobufClient) {
        this.protobufClient = protobufClient;
    }

    @Override
    public RemotingCommand processRequest(ChannelHandlerContext ctx, RemotingCommand request) throws InvalidProtocolBufferException {
        ProtoMsg.Message outmsg = ProtoMsg.Message.parseFrom(request.getBody());
        if (outmsg.getType() == ProtoMsg.HeadType.SCANCODEREQ) {
            ProtoMsg.ScanCodeReq scanCodeReq = outmsg.getScanCodeReq();
            log.info("扫码：{}", scanCodeReq.getOpenFlag());
            RemotingCommand remotingCommand = new RemotingCommand();
            remotingCommand.setOpaque(request.getOpaque());
            ProtoMsg.Message.Builder builder = ProtoMsg.Message.newBuilder();
            ProtoMsg.ScanCodeRes.Builder scanCodeRes = ProtoMsg.ScanCodeRes.newBuilder();
            scanCodeRes.setScanStatus(1);
            builder.setType(ProtoMsg.HeadType.SCANCODERES);
            builder.setScanCodeRes(scanCodeRes);
            remotingCommand.setBody(builder.build().toByteArray());
            ctx.channel().writeAndFlush(remotingCommand);
        }
        return null;
    }
}
